package name.marz.to.go.btree.node;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import test.Data;


public class ArrayNodeTest {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}
	
	@Test
	public void testConstructor() {
		Node node2 = new ArrayNode(2);
		assertEquals(2, node2.getMaxKeys());
		assertEquals(0, node2.getKeyCount());
		assertNull(node2.getKey(0));
		assertNull(node2.getPointer(0));
		assertNull(node2.getKey(1));
		assertNull(node2.getPointer(1));
	}
	
	@Test
	public void testAdd() {
		Node node2 = new ArrayNode(2);
		try {
			node2.add(new Key(1), "Key1");
			assertEquals(1, node2.getKeyCount());
			assertEquals(1, node2.getKey(0).getValue());
			assertEquals("Key1", node2.getPointer(0));
			node2.add(new Key(2), "Key2");
			assertEquals(2, node2.getKeyCount());
			assertEquals(2, node2.getKey(1).getValue());
			assertEquals("Key2", node2.getPointer(1));
		} catch (NodeOverflowException e) {
			fail(e.getClass().getName());
		}
		
	}

	@Test
	public void testSplit() {
		Node node2 = new ArrayNode(2);
		try {
			node2.add(new Key(1), "Key1");
			node2.add(new Key(2), "Key2");
			Node larger_node = node2.split();
			assertEquals(1, node2.getKeyCount());
			assertEquals(1, larger_node.getKeyCount());
			assertEquals(1, node2.getKey(0).getValue());
			assertEquals(2, larger_node.getKey(0).getValue());
			assertEquals("Key1", node2.getPointer(0));
			assertEquals("Key2", larger_node.getPointer(0));
		} catch (NodeOverflowException e) {
			fail(e.getClass().getName());
		}
	}
	
	@Test
	public void testSet() {
		ArrayNode node = new ArrayNode(2);
		
		Key key1 = new Key(1);
		Data data1 = new Data(1);
		Key key2 = new Key(2);
		Data data2 = new Data(2);
		
		try {
			node.add(key1, data1);
			assertEquals(node.getPointer(key1),data1);
			node.add(key2, data2);
			assertEquals(node.getPointer(key1),data1);
			assertEquals(node.getPointer(key2),data2);
		} catch (NodeOverflowException e) {
			e.printStackTrace();
			fail("オーバーフローしないはず");
			return;
		}
		try {
			node.add(new Key(3), new Data(3));
			fail("オーバーフローするはず");
		} catch (NodeOverflowException e) {
//			e.printStackTrace();
		}
	}
	@Test
	public void testClone() {
		ArrayNode node = new ArrayNode(2);
		
		Key key1 = new Key(1);
		Data data1 = new Data(1);
		Key key2 = new Key(2);
		Data data2 = new Data(2);
		
		try {
			node.add(key1, data1);
			node.add(key2, data2);
			
			ArrayNode clone = (ArrayNode)node.clone();
			
			assertEquals(key1.getValue(),clone.getKey(0).getValue());
			assertEquals(key2.getValue(),clone.getKey(1).getValue());
			assertEquals(data1,clone.getPointer(0));
			assertEquals(data2,clone.getPointer(1));
			assertNotSame(node,clone);
		} catch (NodeOverflowException e) {
			e.printStackTrace();
			fail("オーバーフローしないはず");
			return;
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getClass().getName()+"は発生しないはず");
			return;
		}
	}

}
